package com.genesis.yunnanji.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.activity.Pavilion;
import com.genesis.yunnanji.activity.Search;
import com.genesis.yunnanji.adapter.TabAdapter;
import com.genesis.yunnanji.utils.GenesisUtils;

import org.xutils.view.annotation.ContentView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/11/7.
 */
@ContentView(R.layout.fragment_homepage)
public class HomePage extends Fragment {
    private ViewPager mViewPager;
    private View view;
    private GenesisUtils genesisUtils;
    private TabAdapter adapter;
    public String[] tabTitle;
    private TabLayout mTabLayout;
    private TextView tvSearch;

    public static HomePage newInstance(String param) {
        HomePage obj = new HomePage();
        Bundle bundle = new Bundle();
        bundle.putString("args1", param);
        obj.setArguments(bundle);
        return obj;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = this.getActivity().getLayoutInflater().inflate(R.layout.fragment_homepage, null);
//            StatusBarUtil.setColor(getActivity(), getResources().getColor(R.color.main_color), 0);
            tabTitle = getResources().getStringArray(R.array.tabtitle);
            genesisUtils = GenesisUtils.getIntance();
            mViewPager = (ViewPager) view.findViewById(R.id.vp_homepage_viewpager);
            mTabLayout = (TabLayout) view.findViewById(R.id.tl_homepage_tab);
            tvSearch = (TextView) view.findViewById(R.id.et_homepage_search);
            initFragment();
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }
        return view;
    }

    private void initFragment() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(NewsHome.newInstance("1"));
        fragments.add(Subject_Video_Visit.newInstance(1));
        fragments.add(Subject_Video_Visit.newInstance(2));
        fragments.add(Subject_Video_Visit.newInstance(3));
//        fragments.add(MushRoom.newInstance(1));
//        fragments.add(MushRoom.newInstance(2));
//        fragments.add(MushRoom.newInstance(3));
        fragments.add(SpecialtyFragment.newInstance(1));
        fragments.add(SpecialtyFragment.newInstance(2));
        fragments.add(SpecialtyFragment.newInstance(3));
//        fragments.add(Pavilion.newInstance(""));
        fragments.add(PavilionFragment.newInstance(""));
        adapter = new TabAdapter(getActivity().getSupportFragmentManager(), fragments, tabTitle);
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        mViewPager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mViewPager);
        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Search.class);
                startActivity(intent);
            }
        });
    }
}
